<?php

/**
 * ECSHOP 商品类型管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: goods_type.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($ecs->table("series"), $db, 'series_id', 'series_name');

/*------------------------------------------------------ */
//-- 管理界面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'manage')
{	admin_priv('series_manage');
    assign_query_info();

    $smarty->assign('ur_here',          $_LANG['series']);
    $smarty->assign('full_page',        1);

    $series_list = get_goodstype();
    $good_in_type = '';	foreach($series_list['type'] as $key => $val)	{		$series_list['type'][$key]['series_desc']=mb_substr($val['series_desc'],0,35,'utf-8');	}		$brand = $db->getAll("SELECT brand_id,brand_name from ".$GLOBALS['ecs']->table('brand')." order by brand_short,sort_order");		$smarty->assign('brand',   $brand);	
    $smarty->assign('series_list_info',   $series_list['type']);
    $smarty->assign('filter',       $series_list['filter']);
    $smarty->assign('record_count', $series_list['record_count']);
    $smarty->assign('page_count',   $series_list['page_count']);

    $smarty->assign('action_link',      array('text' => $_LANG['add_series'], 'href' => 'series.php?act=add'));

    $smarty->display('series_list.htm');
}

/*------------------------------------------------------ */
//-- 获得列表
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $series_list = get_goodstype();
	foreach($series_list['type'] as $key => $val)	{		$series_list['type'][$key]['series_desc']=mb_substr($val['series_desc'],0,35,'utf-8');	}		$brand = $db->getAll("SELECT brand_id,brand_name from ".$GLOBALS['ecs']->table('brand')." order by brand_short,sort_order");		$smarty->assign('brand',   $brand);	
    $smarty->assign('series_list_info',   $series_list['type']);
    $smarty->assign('filter',       $series_list['filter']);
    $smarty->assign('record_count', $series_list['record_count']);
    $smarty->assign('page_count',   $series_list['page_count']);

    make_json_result($smarty->fetch('series_list.htm'), '',
        array('filter' => $series_list['filter'], 'page_count' => $series_list['page_count']));
}

/*------------------------------------------------------ */
//-- 修改商品类型名称
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_type_name')
{
    check_authz_json('series_manage');

    $type_id   = !empty($_POST['id'])  ? intval($_POST['id']) : 0;
    $type_name = !empty($_POST['val']) ? json_str_iconv(trim($_POST['val']))  : '';

    /* 检查名称是否重复 */
    $is_only = $exc->is_only('cat_name', $type_name, $type_id);

    if ($is_only)
    {
        $exc->edit("cat_name='$type_name'", $type_id);

        admin_log($type_name, 'edit', 'goods_type');

        make_json_result(stripslashes($type_name));
    }
    else
    {
        make_json_error($_LANG['repeat_type_name']);
    }
}

/*------------------------------------------------------ */
//-- 切换启用状态
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'toggle_enabled')
{
    check_authz_json('series_manage');

    $id     = intval($_POST['id']);
    $val    = intval($_POST['val']);

    $exc->edit("enabled='$val'", $id);

    make_json_result($val);
}

/*------------------------------------------------------ */
//-- 添加商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add')
{
    admin_priv('series_manage');


	$brand = $db->getAll("SELECT brand_id,brand_name from ".$GLOBALS['ecs']->table('brand')." order by brand_short,sort_order");		$smarty->assign('brand',   $brand);
        $smarty->assign('ur_here',     $_LANG['new_series']);

    $smarty->assign('action_link', array('href'=>'series.php?act=manage', 'text' => $_LANG['series_list']));

    $smarty->assign('action',      'add');

    $smarty->assign('form_act',    'insert');

    $smarty->assign('picture_type',  array('enabled' => 1));



    assign_query_info();
    $smarty->display('series_info.htm');
}

elseif ($_REQUEST['act'] == 'insert')
{	admin_priv('series_manage');
    //$goods_type['cat_name']   = trim_right(sub_str($_POST['cat_name'], 60));
    //$goods_type['attr_group'] = trim_right(sub_str($_POST['attr_group'], 255));
//  $picture_name['picture_name']   = sub_str($_POST['picture_name'], 60);
//
//  $picture_name['enabled']    = intval($_POST['enabled']);    $series_name['enabled']    = intval($_POST['enabled']);
	$series_name['series_name'] = sub_str($_POST['series_name'], 60);
	$series_name['series_position'] = sub_str($_POST['series_position'], 60);
	$series_name['series_desc'] = sub_str($_POST['series_desc'], 0);
	$series_name['brand_id'] = sub_str($_POST['brand_id'], 3);
	
    if ($db->autoExecute($ecs->table('series'), $series_name) !== false)
    {		
        $links = array(array('href' => 'series.php?act=manage', 'text' => 系列维护列表));				admin_log($series_name['series_name'], 'add', 'series');		
        sys_msg('新建系列维护成功', 0, $links);
    }
    else
    {
        sys_msg('新建系列维护成功', 1);		
    }
}

/*------------------------------------------------------ */
//-- 编辑商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit')
{	admin_priv('series_manage');
    $series = get_series(intval($_GET['series_id']));

    if (empty($series))
    {
        sys_msg($_LANG['cannot_found_goodstype'], 1);
    }
	$brand_info = $db->getAll("SELECT * from ".$GLOBALS['ecs']->table('brand')." order by brand_short,sort_order");		$smarty->assign('brand',   $brand_info);	
    $smarty->assign('ur_here',     $_LANG['edit_series']);
    $smarty->assign('action_link', array('href'=>'series.php?act=manage', 'text' => $_LANG['series_list']));
    $smarty->assign('action',      'add');
    $smarty->assign('form_act',    'update');
    $smarty->assign('series',  $series);

    assign_query_info();
    $smarty->display('series_info.htm');
}
elseif ($_REQUEST['act'] == 'update')
{	admin_priv('series_manage');

    $series_id                  = intval($_GET['series_id']);	$series_name['enabled']    = intval($_POST['enabled']);	$series_name['series_name'] = sub_str($_POST['series_name'], 60);	$series_name['series_position'] = sub_str($_POST['series_position'], 60);	$series_name['series_desc'] = sub_str($_POST['series_desc'], 0);	$series_name['brand_id'] = sub_str($_POST['brand_id'], 3);
    if ($db->autoExecute($ecs->table('series'), $series_name, 'UPDATE', "series_id='$series_id'") !== false)
    {

        $links = array(array('href' => 'series.php?act=manage', 'text' => $_LANG['series_list']));		admin_log($series_name['series_name'], 'edit', 'series');		
        sys_msg(系列维护更新成功, 0, $links);
    }
    else
    {
        sys_msg(系列维护更新失败, 1);
    }
}

/*------------------------------------------------------ */
//-- 删除商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'remove')
{
//  check_authz_json('picture_type');
	admin_priv('series_delect');
    $id = intval($_GET['id']);
	$sql = "SELECT series_name FROM " .$ecs->table('series'). " WHERE series_id = '$id'";    $logo_name = $db->getOne($sql);
    $name = $exc->get_name($id);

    if ($exc->drop($id))

    {
//      admin_log(addslashes($name), 'remove', 'picture_type');
//		 $GLOBALS['db']->query("DELETE FROM " .$ecs->table('picture_type'). " WHERE picture_id =" .$id );
        /* 清除该类型下的所有属性 */
//      $sql = "SELECT attr_id FROM " .$ecs->table('attribute'). " WHERE cat_id = '$id'";
//
//      $arr = $db->getCol($sql);
//
//
//
//      $GLOBALS['db']->query("DELETE FROM " .$ecs->table('attribute'). " WHERE attr_id " . db_create_in($arr));
//
//      $GLOBALS['db']->query("DELETE FROM " .$ecs->table('goods_attr'). " WHERE attr_id " . db_create_in($arr));
		admin_log($logo_name, 'remove', 'series');
        $url = 'series.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        ecs_header("Location: $url\n");
        exit;
    }
//
    else

    {

        make_json_error($_LANG['remove_failed']);

    }
}

/**
 * 获得所有商品类型
 *
 * @access  public
 * @return  array
 */
function get_goodstype()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();
		$filter['brand_id']         = empty($_REQUEST['brand_id']) ? 0 : intval($_REQUEST['brand_id']);       			$filter['keyword']          = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);				$where='';				if($filter['brand_id']!=0){			$where .= " AND t.brand_id='$filter[brand_id]'";		}		        /* 关键字 */        if (!empty($filter['keyword']))        {            $where .= " AND (t.series_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%' )";        }
        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('series'). "as t where t.series_id!=0 $where ";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 *///		$sql="SELECT * from ".$GLOBALS['ecs']->table('series');
//      $sql = "SELECT t.*, COUNT(a.cat_id) AS attr_count ".
//
//             "FROM ". $GLOBALS['ecs']->table('goods_type'). " AS t ".
//
//             "LEFT JOIN ". $GLOBALS['ecs']->table('attribute'). " AS a ON a.cat_id=t.cat_id ".
//
//             "GROUP BY t.cat_id " .
//
//             'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];        $sql = "SELECT t.*, a.brand_name ".

               "FROM ". $GLOBALS['ecs']->table('series'). " AS t ".

               "LEFT JOIN ". $GLOBALS['ecs']->table('brand'). " AS a ON a.brand_id=t.brand_id ".
								" where t.series_id!=0 $where ".				
//             "GROUP BY t.brand_id " .

               'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $all = $GLOBALS['db']->getAll($sql);
    return array('type' => $all, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 获得指定的商品类型的详情
 *
 * @param   integer     $cat_id 分类ID
 *
 * @return  array
 */
function get_series($series_id)
{
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('series'). " WHERE series_id='$series_id'";

    return $GLOBALS['db']->getRow($sql);
}

?>
